/*
 * Copyright © Yan Zhenjie. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yanzhenjie.permission;

import android.app.Activity;
import android.content.DialogInterface;

import com.lwkandroid.rtpermission.ui.RTDialog;

import java.util.List;

import androidx.annotation.NonNull;

/**
 * <p>Default Setting Dialog, have the ability to open Setting.</p>
 * Created by Yan Zhenjie on 2016/12/27.
 */
public class SettingDialog {

    private RTDialog.Builder mBuilder;
    private SettingService mSettingService;
    private Activity activity;

//    public SettingDialog(int themrResId,@NonNull Context context, @NonNull SettingService settingService) {
//        mBuilder = AlertDialog.newBuilder(context,themrResId)
//                .setCancelable(false)
//                .setTitle(R.string.permission_title_permission_failed)
//                .setMessage(R.string.permission_message_permission_failed)
//                .setPositiveButton(R.string.permission_setting, mClickListener)
//                .setNegativeButton(R.string.permission_cancel, mClickListener);
//        this.mSettingService = settingService;
//    }

    public SettingDialog(@NonNull Activity activity, List<String> needRequestList, @NonNull SettingService settingService) {
        String appName ="";// RTUtils.getAppName(this.getApplicationContext());
        String message = activity.getString(R.string.rtpermission_nerverask_dialog_message, appName);
        mBuilder=new RTDialog.Builder()
                .permissions(needRequestList)
                .message(message)
                .title(activity.getString(R.string.rtpermission_nerverask_dialog_title))
                .negativeButton(R.string.rtpermission_nerverask_dialog_cancel, mClicknegListener)
                .positiveButton(R.string.rtpermission_nerverask_dialog_setting, mClickPosListener);
//
//        AlertDialog.newBuilder(context)
//                .setCancelable(false)
//                .setTitle(R.string.permission_title_permission_failed)
//                .setMessage(R.string.permission_message_permission_failed)
//                .setPositiveButton(R.string.permission_setting, mClickListener)
//                .setNegativeButton(R.string.permission_cancel, mClickListener);
        this.mSettingService = settingService;
        this.activity=activity;
    }

//    @NonNull
//    public SettingDialog setTitle(@NonNull String title) {
//        mBuilder.setTitle(title);
//        return this;
//    }
//
//    @NonNull
//    public SettingDialog setTitle(@StringRes int title) {
//        mBuilder.setTitle(title);
//        return this;
//    }
//
//    @NonNull
//    public SettingDialog setMessage(@NonNull String message) {
//        mBuilder.setMessage(message);
//        return this;
//    }
//
//    @NonNull
//    public SettingDialog setMessage(@StringRes int message) {
//        mBuilder.setMessage(message);
//        return this;
//    }
//
//    @NonNull
//    public SettingDialog setNegativeButton(@NonNull String text, @Nullable DialogInterface.OnClickListener
//            negativeListener) {
//        mBuilder.setNegativeButton(text, negativeListener);
//        return this;
//    }
//
//    @NonNull
//    public SettingDialog setNegativeButton(@StringRes int text, @Nullable DialogInterface.OnClickListener
//            negativeListener) {
//        mBuilder.setNegativeButton(text, negativeListener);
//        return this;
//    }
//
//    @NonNull
//    public SettingDialog setPositiveButton(@NonNull String text) {
//        mBuilder.setPositiveButton(text, mClickListener);
//        return this;
//    }
//
//    @NonNull
//    public SettingDialog setPositiveButton(@StringRes int text) {
//        mBuilder.setPositiveButton(text, mClickListener);
//        return this;
//    }

    public void show() {
        mBuilder.show(activity);
    }

    /**
     * The dialog's btn click listener.
     */
    private DialogInterface.OnClickListener mClickPosListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            mSettingService.execute();
        }
    };
    private DialogInterface.OnClickListener mClicknegListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            mSettingService.cancel();
            activity.finish();
        }
    };
}
